<!DOCTYPE html>
<html>
<head>
	<title>digui</title>
</head>
<body>
	<script type="text/javascript">
		var arr = [
			{id:1, parentId:0,name:"1",chil:[]},
			{id:6, parentId:0,name:"2",chil:[]},
			{id:2, parentId:1,name:"3",chil:[]},
			{id:3, parentId:1,name:"4",chil:[]},
			{id:4, parentId:2,name:"5",chil:[]},
			{id:15, parentId:4,name:"15",chil:[]},
			{id:5, parentId:2,name:"6",chil:[]},
			{id:7, parentId:5,name:"7",chil:[]},
			{id:8, parentId:7,name:"8",chil:[]},
			{id:9, parentId:7,name:"9",chil:[]},
			{id:10, parentId:9,name:"10",chil:[]},
			{id:11, parentId:9,name:"11",chil:[]},
			{id:12, parentId:6,name:"12",chil:[]},
			{id:13, parentId:6,name:"13",chil:[]},
			{id:14, parentId:13,name:"14",chil:[]}
		]

		/**

		[
			{"id":1,"parentId":0,"name":"1","chil":[
				{"id":2,"parentId":1,"name":"3","chil":[
						{"id":4,"parentId":2,"name":"5","chil":[]},
						{"id":5,"parentId":2,"name":"6","chil":[
							{"id":7,"parentId":5,"name":"7","chil":[]}
							]
						}
					]
				},
				{"id":3,"parentId":1,"name":"4","chil":[]}]},
			{"id":6,"parentId":0,"name":"2","chil":[]}
		]


		*/
		var x = 0;
		function digui(arr,arr1) {
			var arr2 = [];
			if(arr1.length == 0){
				for(var i=0; i<arr.length; i++){
					if(arr[i].parentId == 0){
						arr1.push(arr[i]);
					}
				}
				digui(arr, arr1);
			}else{
				for(var j=0; j<arr1.length; j++){
					for(var k=0; k<arr.length; k++){
						if(arr1[j].id == arr[k].parentId){
							arr1[j].chil.push(arr[k]);
							arr2.push(arr[k]);
						}
					}
				}
				if(arr2.length == 0){
					return ;
				}
				digui(arr, arr2);
			}
			return arr1;
		}

		var re = digui(arr, []);
		var h = 1
		function shu(re, index){
			var html = "";
			var mark = true;
			for(var i=0; i<re.length; i++){
				if(mark){
					index++;
					mark = false;
				}
				html += "<div style='text-indent:"+index*20+"px'>" + re[i].name;
				if(re[i].chil.length == 0){
					html += "</div>";
					continue ;
				}else{
					
					html += shu(re[i].chil, index);

				}
				html += "</div>";
			}

			return html;
		}

		var h = shu(re, 1);
		document.body.innerHTML = h;
	</script>
</body>
</html>